package org.csc.sss.admin.controller.base;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.csc.sss.admin.config.SmsTplConfig;
import org.csc.sss.datacore.annontation.WebLog;
import org.csc.sss.datacore.config.ValidateConfig;
import org.csc.sss.datacore.dto.PersonDTO;
import org.csc.sss.datacore.exception.SssException;
import org.csc.sss.datacore.primary.model.*;
import org.csc.sss.util.ExcelData;
import org.csc.sss.util.ExportExcelUtils;
import org.csc.sss.util.PasswordEncoderUtils;
import org.csc.sss.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Api("公开")
@RestController
@RequestMapping(value = "public")
public class PublicController extends BaseController {

//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;

//    @Autowired
//    private RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    private SmsTplConfig smsTplConfig;


//    @RequestMapping(value = "redis")
//    public Result<?> redis() {
//
//        stringRedisTemplate.opsForValue().set("sss:redis","测试222", 5, TimeUnit.SECONDS);
//
//        return super.ok();
//    }

    @RequestMapping(value = "test")
    public Result<Map<String, Object>> test() {


        Map<String, Object> map = new HashMap<>();
        map.put("1", smsTplConfig.getDebug());
        map.put("2", smsTplConfig.getProvider());
        map.put("3", smsTplConfig.getSign());
        map.put("4", smsTplConfig.getTpls());

        return super.ok(map);
    }

    @RequestMapping(value = "validated")
    public Result<?> validated(@Validated(ValidateConfig.Person.ADD.class) Person person) {


        return super.ok();
    }

    @RequestMapping(value = "up")
    public Result<?> up() {
        PersonDTO dto = new PersonDTO();
        dto.setAddress("jpa动态更新");
        dto.setId("402880e6632dec3801632dec8cb90000");
//        personRepositoryImpl.test(dto);
        return super.ok(personRepositoryImpl.test(dto));
    }

    @RequestMapping(value = "registered")
    public Result<?> registered(@RequestParam(required = true) String username, @RequestParam(required = true) String password) {
        SysUser user = new SysUser();
        user.setNickname(username);
        user.setPswd(PasswordEncoderUtils.passwordEncoder(password));
        user.setEmail("2132@11");
        user.setCreateTime(new Date());
        user.setStatus(1);
        SysUser sysUser = sysUserRepository.save(user);
        if (null == sysUser) {
            throw new RuntimeException("注册失败");
        }
        return super.ok();
    }

    @RequestMapping(value = "doAdd.do")
    @Transactional
    public Result<?> doAdd() {

        SysRole role = new SysRole();
        role.setCode("ADMIN");
        role.setName("登录的权限");
        SysRole sysRole = sysRoleRepository.save(role);
        if (null == sysRole) {
            throw new SssException(Result.ResultCode.INSERT_DATA_ERROR);
        }
        SysUserRole userRole = new SysUserRole();
        userRole.setRoleId(sysRole.getId());
        userRole.setUserId("40289f1f62c70b920162c70e103f0000");
        SysUserRole sysUserRole = sysUserRoleRepository.save(userRole);
        if (null == sysUserRole) {
            throw new SssException(Result.ResultCode.INSERT_DATA_ERROR);
        }
        SysPermission permission = new SysPermission();
        permission.setName("sys.admin.login");
        permission.setDescription("登录的权限");
        permission.setPid("1");
        permission.setUrl("/admin/**");
        SysPermission sysPermission = sysPermissionRepository.save(permission);
        if (null == sysPermission) {
            throw new SssException(Result.ResultCode.INSERT_DATA_ERROR);
        }
        SysRolePermission rolePermission = new SysRolePermission();
        rolePermission.setPermissionId(sysPermission.getId());
        rolePermission.setRoleId(sysRole.getId());
        SysRolePermission sysRolePermission = sysRolePermissionRepository.save(rolePermission);
        if (null == sysRolePermission) {
            throw new SssException(Result.ResultCode.INSERT_DATA_ERROR);
        }

        return super.ok();
    }

    @ApiOperation("查询人列表")
    @WebLog("查询人列表")
    @RequestMapping(value = "personList.json")
    public Result<List<Person>> personList(){

        PersonDTO dto = new PersonDTO();
        //</editor-fold>
//        dto.setAge(11);
        SysUser sysUser = new SysUser();
        sysUser.setNickname("%1%");
//		List<Integer> ages = new ArrayList<Integer>();
//		ages.add(10);
//		ages.add(14);
//		List<Integer> ids = new ArrayList<Integer>();
//		ids.add(1);
//		ids.add(2);
//		dto.setAges(ages);
//		dto.setIds(ids);
//		List<Person> findAll = personRepository.findAll(personRepositoryImpl.personSpecification(dto));
//		personRepositoryImpl.a();


        return super.ok(personRepository.findAll(personRepositoryImpl.personSpecification(dto)));
    }

    @RequestMapping(value = "/excel", method = RequestMethod.GET)
    public void excel(HttpServletResponse response) throws Exception {
        ExcelData data = new ExcelData();
        data.setName("hello");
        List<String> titles = new ArrayList();
        titles.add("a1");
        titles.add("a2");
        titles.add("a3");
        data.setTitles(titles);

        List<List<Object>> rows = new ArrayList();
        List<Object> row = new ArrayList();
        row.add("11111111111");
        row.add("22222222222");
        row.add("3333333333");
        rows.add(row);

        data.setRows(rows);


        //生成本地
        /*File f = new File("c:/test.xlsx");
        FileOutputStream out = new FileOutputStream(f);
        ExportExcelUtils.exportExcel(data, out);
        out.close();*/
        ExportExcelUtils.exportExcel(response,"hello.xlsx", data);
    }



}
